package com.kuwou.workobject.Utils;

import com.kuwou.workobject.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ExecuteCrawling {

    private final SingerListService singerListService;
    private final SingerBasicInfoService singerBasicInfoService;
    private final SingerDetailInfoService singerDetailInfoService;
    private final MusicService musicService;
    private final RankListService rankListService;
    private final ChannelService channelService;
    private final CommentService commentService;


    // 用 static 变量持有实例引用
    private static ExecuteCrawling instance;

    // 构造函数中初始化 static 实例
    @Autowired
    public ExecuteCrawling(SingerListService singerListService, SingerBasicInfoService singerBasicInfoService,
                           SingerDetailInfoService singerDetailInfoService,
                           MusicService musicService, RankListService rankListService,
                           ChannelService channelService, CommentService commentService) {
        this.singerListService = singerListService;
        this.singerBasicInfoService = singerBasicInfoService;
        this.singerDetailInfoService = singerDetailInfoService;
        this.musicService = musicService;
        this.rankListService = rankListService;
        this.channelService = channelService;
        this.commentService = commentService;

        // 将当前实例赋值给 static 变量
        ExecuteCrawling.instance = this;
    }

    /**
     * 爬取歌手相关数据
     */
    public static void getSingerData(){
        CrawlSingerUtils.setSingerListService(instance.singerListService);
        CrawlSingerUtils.setSingerDetailInfoService(instance.singerDetailInfoService);
        CrawlSingerUtils.setSingerBasicInfoService(instance.singerBasicInfoService);
        CrawlSingerUtils.execute();
    }

    /**
     * 爬取排行榜相关数据
     */
    public static void getRankListData() throws IOException {
        CrawlRankListDataUtils.setMusicService(instance.musicService);
        CrawlRankListDataUtils.setRankListService(instance.rankListService);
        CrawlRankListDataUtils.setChannelService(instance.channelService);
        CrawlRankListDataUtils.setCommentService(instance.commentService);
        CrawlRankListDataUtils.getData();
        CrawlRankListDataUtils.getComment();
    }


}
